#region imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
#endregion

namespace PHSRAG.Insight21.Humans
{
	/// <summary>
	/// The PendingApplicationSummary class is the facade over the details for a single
	/// pending application summary.
	/// </summary>
	public class PendingApplicationSummary : BO.BusinessObject
	{
		#region Constants
		private const string	PendingAppSummaryKey = "pendingAppSummary_5547A6D0-9766-487b-A051-B11113D4C326";
		private const string	TableName = "PendingApplicationSummary";
		private const int		MaxTitleLength = 30;
		private const int		MaxSponsorLength = 30;
		#endregion

		#region Instance Variables
		private DataRow	dataRow;
		#endregion


		#region Constructor
		/// <summary>
		/// Initialize the base class with the specified cache policy, and save the DataRow in this instance.
		/// This is an example of a scenario where even though the AgreementSummary IS a business object, it DOES NOT
		/// use the DataContainer to store anything. The DataContainer is typically used when the BusinessObject is itself
		/// responsibly for loading the data for which it is the facacde. However, we have a scenario where the data for
		/// this object is already loaded (by the AgreementSummaryies object), and the AgreementSummary object simply
		/// provides the facade.
		/// </summary>
		/// <param name="cachePolicy">Prevalent cache policy</param>
		/// <param name="row">DataRow for the facade</param>
		public PendingApplicationSummary(ICachePolicy cachePolicy, DataRow row) : base(cachePolicy)
		{
			dataRow = row;
		}

		#endregion

		#region Properties

		/// <summary>
		/// Gets the ID of the Protocol that the application belongs to
		/// </summary>
		public int ProtocolID 
		{
			get
			{
                object o = dataRow["ProtocolId"];
                return (o == DBNull.Value)? 0: (int)o;
			}
		}

		/// <summary>
		/// Gets the access level the logged-in user has to the protocol
		/// </summary>
		public int AccessLevel
		{
			get
			{
				return (int)dataRow["AccessLevel"];
			}
		}

		/// <summary>
		/// Gets the FormsID of the submission (e.g. CR2); This will be null (in the database) for FormsID
		/// </summary>
		public string FormsID
		{
			get
			{
				object o = dataRow["FormsID"];
				return (o == DBNull.Value) ? "" : (string)o;	
			}
		}

		/// <summary>
		/// Gets the long title of the Protocol
		/// </summary>
		public string Title
		{
			get
			{
				return (string)dataRow["Title"];
			}
		}

		/// <summary>
		/// Gets a shortened version of the long Title (truncates to 30 chars if longer)
		/// </summary>
		public string TruncatedTitle
		{
			get
			{
				string longTitle = Title;
				return (longTitle.Length > MaxTitleLength) ? (longTitle.Substring(0, MaxTitleLength - 3) + "...") : longTitle;
			}
		}

		/// <summary>
		/// Gets the name of the sponsor on the protocol
		/// </summary>
		public string SponsorName
		{
			get
			{		
				object o = dataRow["SponsorName"];
				return (o == DBNull.Value) ? "" : (string)o;	
			}
		}
		
		/// <summary>
		/// Gets a shortened version of the SponsorName (truncates to 30 chars if longer)
		/// </summary>
		public string TruncatedSponsorName
		{
			get
			{
				string longSponsor = SponsorName;
				return (longSponsor.Length > MaxSponsorLength) ? 
					(longSponsor.Substring(0, MaxSponsorLength - 3) + "...") : longSponsor;
			}
		}

		/// <summary>
		/// Gets the number of the Protocol as assigned by REX
		/// </summary>
		public string Number
		{
			get
			{ 
				object o = dataRow["ProtocolNumber"];
				return (o == DBNull.Value) ? "" : (string)o;	
			}
		}

		/// <summary>
		/// Gets the name of the Principal Investigator on the Protocol in Last Name, First Name format
		/// </summary>
		public string PIName
		{
			get
			{
				object o = dataRow["PIName"];
				return (o == DBNull.Value) ? "" : (string)o;
			}
		}

		/// <summary>
		/// Gets the date the Protocol was submitted to the IRB
		/// </summary>
		public string SubmissionDate
		{
			get
			{ 
				object o = dataRow["SubmissionDate"];
				return (o == DBNull.Value) ? "" : ((DateTime)o).ToString("MM/dd/yyyy");
			}		
		}

		/// <summary>
		/// Gets the name of the board performing the review
		/// </summary>
		public string BoardName
		{
			get
			{ 
				object o = dataRow["BoardName"];
				return (o == DBNull.Value) ? "" : (string)o;
			}
		}

		/// <summary>
		/// Gets the type of the review (e.g. Full, Expedited, Exempt)
		/// </summary>
		public string ReviewType
		{
			get
			{ 
				object o = dataRow["ReviewType"];
				return (o == DBNull.Value) ? "" : (string)o;
			}
		}

		/// <summary>
		/// Gets the date the submission will be reviewed
		/// </summary>
		public string MeetingDate
		{
			get
			{ 
				object o = dataRow["MeetingDate"];
				return (o == DBNull.Value) ? "" : ((DateTime)o).ToString("MM/dd/yyyy");
			}		
		}

		/// <summary>
		/// Gets the status of the submission handed down by the board
		/// </summary>
		public string BoardStatus
		{
			get
			{
				object o = dataRow["BoardStatus"];
				return (o == DBNull.Value) ? "" : (string)o;
			}
		}
		/// <summary>	
		/// Gets whether a protocol has been locked (i.e. changes cannot be made to it)
		/// </summary>
		public bool IsLocked
		{
			get
			{
                object o = dataRow["IsLocked"];
                return (o == DBNull.Value) ? false : (bool)o;
			}
		}

		/// <summary>
		/// Gets the id of the submission
		/// </summary>
		public int SubmissionId
		{
			get
			{
				object o = dataRow["SubmissionId"];
				return (o == DBNull.Value) ? 0 : (int) o;
			}
		}

		/// <summary>
		/// Returns true if there is notification text on the submission
		/// </summary>
		public bool Notification
		{
			get
			{
				return Convert.ToBoolean(dataRow["Notification"]);
			}
		}

		/// <summary>
		/// Gets the id of the board review
		/// </summary>
		public int BoardReviewId
		{
			get
			{
				object o = dataRow["BoardReviewId"];
				return (o == DBNull.Value) ? 0 : (int) o;
			}
		}

		/// <summary>
		/// Gets Protocol Process ID from submission
		/// </summary>
		public int ProtocolProcessId
		{
			get
			{
				object o = dataRow["ProtocolProcessId"];
				return (o == DBNull.Value) ? 0 : (int)o;				
			}
		}
		#endregion Properties

		#region Base Methods
		/// <summary>
		/// Cache this instance using the prevalent cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(PendingAppSummaryKey, this);
		}

		/// <summary>
		/// Remove this instance from the cache using the prevalent cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(PendingAppSummaryKey);
		}

		/// <summary>
		/// This method is not used and only exists to satisfy interface requirements.
		/// </summary>
		/// <param name="connection">Connection string to the database</param>
		/// <param name="args">Optional arguments for load</param>
		public override void Load(string connectionString, params object[] args)
		{
			throw new Exception("PendingApplicationSummary is not directly loadable");
		}
		#endregion
	}
}
